<template>
  <el-dialog
    width="30%"
    title="选择应用"
    :visible.sync="params.visible"
  >
    <compTree
      ref="tree"
      :tree-show-checkbox="true"
      :tree-data="treeData"
      :highlight-current="true"
      :show-search="false"
      :comp-tree-height="400"
      :tree-default-checked-keys="treeDefaultCheckedKeys"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>
<script>
/**
 * 入参：
 *    params: {
 *        targetNames,
 *        isNullSelect,
 *        computerName,
 *        programName
 *    }
 */
import { getTargetList } from '@/api/core'
import { SQLTable, ParamArr } from '../../../public/js/paramarr'
import Global from '../../../public/js/global'
export default {
  name: 'SelectTargetFilterProgram',
  props: {
    params: {
      type: Object,
      default() {
        return {
          visible: false,
          targetNames: '',
          isNullSelect: false,
          computerName: '',
          programName: ''
        }
      }
    }
  },
  data() {
    return {
      checked: false,
      formData: '',
      treeData: [],
      nodeCheck: [],
      resule: '',
      treeDefaultCheckedKeys: [],
      cmtProgram: '3'
    }
  },
  created() {
    this.formData = this.params
    const vPar = new ParamArr()
    if (this.formData.programName !== undefined && this.formData.programName !== '') {
      vPar.setStr('Programs', this.formData.programName)
    }
    if (this.formData.computerName !== undefined && this.formData.computerName !== '') {
      vPar.setStr('IPs', this.formData.computerName)
    }
    getTargetList(vPar).then(vPar => {
      this.lpfInitWindow(vPar)
    })
  },
  methods: {
    // open(ok) {
    //   this.ok = ok
    //   this.formData = this._.cloneDeep(this.params)

    // },
    lpfInitWindow(vPar) {
      var nodes = []
      var nodeMap = new Map()
      let sName = ''
      var table = new SQLTable(vPar.getTable('k_table'))
      table.first()
      while (!table.eof()) {
        if (table.getValueByName('PName') === '') sName = table.getValueByName('Name')
        else sName = Global.strFormat('{0}.{1}', table.getValueByName('PName'), table.getValueByName('Name'))
        var pnode = Global.getTreeviewPNode(nodeMap, nodes, '')
        var node = {}
        node['id'] = Global.getGuid()
        node['text'] = sName
        node['name'] = sName
        node['icon'] = 'yingyong2'
        // add
        pnode.push(node)
        // next
        table.next()
      }
      this.treeData = nodes
      // 数据回显
      if (this.formData.targetNames !== undefined && this.formData.targetNames !== '') {
        for (let i = 0; i < nodes.length; i++) {
          if (Global.strFormat('{0}{1}{2}', ',', this.formData.targetNames, ',').indexOf(Global.strFormat('{0}{1}{2}', ',', nodes[i].name, ',')) !== -1) {
            this.treeDefaultCheckedKeys.push(nodes[i].id)
          }
        }
      }
    },
    getReturn() {
      var arrName = []
      const nodeCheck = this.$refs.tree.getCheckedNodes()
      for (var i = 0; i < nodeCheck.length; i++) {
        var node = nodeCheck[i]
        if (node.id !== '') {
          arrName.push(node.name)
        }
      }
      var obj = {}
      obj.arrName = arrName
      return obj
    },
    confirm() {
      const nodeCheck = this.$refs.tree.getCheckedNodes()
      if (nodeCheck.length === 0 && !this.formData.isNullSelect) return this.$message.warning('请选择指标')
      if (nodeCheck.length === 0) return this.$message.warning('请选择指标')
      const result = this.getReturn()
      if (this.params.confirm !== undefined) {
        this.params.confirm(result)
      } else {
        this.$emit('confirm', result)
      }
    }
  }
}
</script>
